package com.leifeng.security.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.leifeng.security.dto.User;
import com.leifeng.security.dto.UserQueryCondition;
import com.leifeng.security.exception.UserNotExistException;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/user")
public class UserController {
	
	private Logger logger=LoggerFactory.getLogger(getClass());
	
	@GetMapping("/list")
	@JsonView(User.UserSimpleView.class)
	@ApiOperation(value="用户信息列表")
	public 	List<User> query(UserQueryCondition userQueryCondition,@PageableDefault(page=2,size=17,sort="username,asc" )Pageable pageable){
		System.out.println("-------------------------------------------");
		System.out.println(userQueryCondition);
		List<User> list=new ArrayList<User>();
		list.add(new User(1,"张三","123234",new Date()));
		list.add(new User(2,"李四","23123",new Date()));
		list.add(new User(3,"王二小","23123",new Date()));
		
		return list;
	}
	
	@GetMapping("/detail/{id:\\d+}")
	@JsonView(User.UserDetailView.class)
	@ApiOperation(value="用户详细信息")
    public User getInfo(@PathVariable(name="id") String id) {
		
		System.out.println("进入getInfo服务");
		System.out.println(id);
    	User user=new User();
    	user.setUserName("tom");
    	user.setBirthday(new Date());
    	return user;
    }
	
	@PostMapping("/create")
	public User createUser(@Valid @RequestBody  User user,BindingResult error) {
		if(error.hasErrors()) {
			error.getAllErrors().stream().forEach(err ->System.out.println(err.getDefaultMessage()));
		}
		System.out.println(user.getId());
		System.out.println(user.getUserName());
		System.out.println(user.getPassWord());
		System.out.println(user.getBirthday());
		user.setId(1);
		return user;
	}
	
	@PutMapping("/update/{id:\\d+}")
	public User updateUser(@ApiParam(value="用户Id") @PathVariable(name="id") Integer id,@Valid @RequestBody  User user,BindingResult error) {
		if(error.hasErrors()) {
			error.getAllErrors().stream().forEach(err ->{
				   FieldError fieldError=(FieldError)err;
				   String message=fieldError.getField()+" "+fieldError.getDefaultMessage();
				   System.out.println(message);
				});
		}
		System.out.println(user.getId());
		System.out.println(user.getUserName());
		System.out.println(user.getPassWord());
		System.out.println(user.getBirthday());
		user.setId(id);
		return user;
	}
	
	@DeleteMapping("/delete/{id:\\d+}")
	public void deleteUser(@PathVariable(name="id") Integer id) {
		throw new UserNotExistException(id);
		//System.out.println(id);
	}
	
	@RequestMapping("/order")
	public String order() throws Exception {
		logger.info("主线程开始");
		Thread.sleep(1000);
		logger.info("主线程返回");
		
		return "success";
	}


}
